<template>
  <el-button type="success" @click="excel" size="medium" :loading="excelLoading">
    <span class="el-icon-download">&#xe897;</span>
    导出
  </el-button>
</template>

<script>
import { d_runType, d_serveAll } from '@/utils/dicts.js'
export default {
  name: 'ITVerifyExcel',
  props: {
    contractId: {
      type: String,
      default: () => {
        return ''
      }
    }
  },
  data () {
    return {
      excelLoading: false,
      d_runType: d_runType,
      d_serveAll: d_serveAll

    }
  },
  methods: {
    async excel () {
      this.excelLoading = true
      let res = await this.$http('verifyReportIt', {
        del_flag: '0',
        contract_id: this.contractId,
        ...this.searchlist
      })

      let time = new Date().getTime()
      let filename = '资产明细' + time
      import('@/utils/excel/Export2Excel').then(excel => {
        // 多表头
        const multiHeader = [
          // [
          //   '设备(系统名称)', '数量', '资产运行状态', '资产类型', '设备系统型号',
          //   '设备系统型号', '购置时间', '总购置价格', '过保时间', '制造商',
          //   '承建商', '服务内容', '服务分类', '总运维资金预算', '采购方式',
          //   '总购置价格', '第三方机构建议价格', '第三方机构审核意见', '运维开始时间', '运维结束时间'
          // ]
        ]

        // 表头
        const header = [
          '资产id', '设备(系统名称)', '数量', '资产运行状态', '资产类型', '设备系统型号',
          '购置时间', '总购置价格', '过保时间', '制造商', '承建商',
          '服务分类', '总运维资金预算', '采购方式', '总购置价格', '服务内容',
          '第三方机构建议价格', '第三方机构审核意见', '运维开始时间', '运维结束时间'
        ]

        // 处理格子，列名不能小写
        const merges = [
          // 'A1:A2'
        ]

        // 列宽 0开始
        const cWidth = [
          { c: 0, w: 50 },
          { c: 1, w: 15 },
          { c: 2, w: 15 },
          { c: 3, w: 15 },
          { c: 4, w: 15 },
          { c: 5, w: 15 },
          { c: 6, w: 15 },
          { c: 7, w: 15 },
          { c: 8, w: 15 },
          { c: 9, w: 15 },
          { c: 10, w: 15 },
          { c: 11, w: 15 },
          { c: 12, w: 15 },
          { c: 13, w: 15 },
          { c: 14, w: 15 },
          { c: 15, w: 50 },
          { c: 16, w: 15 },
          { c: 17, w: 15 },
          { c: 18, w: 15 },
          { c: 19, w: 15 }
        ]
        const cellStyle = {
          // '2' : {
          //     '0' : {
          //         wrapText: 1,
          //         horizontal: "right",
          //         vertical: "right",
          //     },
          // },
          // '3' : {
          //     wrapText: 1,
          //     horizontal: "left",
          //     vertical: "left",
          // }
        }

        let data = []
        res.forEach((item, index) => {
          let tmp = []
          tmp.push(item.id)
          tmp.push(item.device_name)
          tmp.push(item.device_num)
          let status = ''
          this.d_runType.forEach(val => {
            if (val.value === item.status) {
              status = val.label
            }
          })
          let device_type = ''
          let service_type = ''
          let other = '17,80,227,328,354,360'

          item.device_type ? (device_type = JSON.parse(item.device_type)) : ''
          item.service_type ? (service_type = JSON.parse(item.service_type)) : ''


          this.d_serveAll.forEach(val => {
            if (val.value === device_type[device_type.length - 1]) {
              if (other.indexOf(device_type[device_type.length - 1]) > -1) {
                device_type = val.label + '-' + item.other
              } else {
                device_type = val.label
              }
            } else if (val.value === service_type[service_type.length - 1]) {
              if (other.indexOf(service_type[service_type.length - 1]) > -1) {
                service_type = val.label + '-' + item.other
              } else {
                service_type = val.label
              }
            }
          })
          let buy_type = item.buy_type === '1' ? '单一来源' : '公开招标'
          tmp.push(status)
          tmp.push(device_type)
          tmp.push(item.device_model)
          tmp.push(this.$util.formatDate(item.purchase_time))
          tmp.push(item.purchase_price)
          tmp.push(item.guarantee_time)
          tmp.push(item.manufacturer)
          tmp.push(item.contactor)
          tmp.push(service_type)
          tmp.push(item.service_pre_amount)
          tmp.push(buy_type)
          tmp.push(item.purchase_price)
          tmp.push(item.service_content)
          tmp.push(item.third_check_amount)
          tmp.push(item.third_check_msg)
          tmp.push(item.serve_start_date)
          tmp.push(item.serve_end_date)

          data.push(tmp)
        })
        const len = data.length || 0
        // 设置了一个合并的单元格
        // const a = 'A' + (len + 8) + ':' + 'AM' + (len + 20);
        // merges.push(a)

        // 字号
        let fontSize = [
          12
        ]
        // 循环遍历字号
        for (var i = 0; i < len; i++) {
          fontSize.push(10)
        }
        // 边框
        const border = [
          { style: 'thin', color: '#999' }
        ]

        for (var j = 0; j < len; j++) {
          border.push({ style: 'thin', color: '#099' })
        }
        excel.export_json_to_excel({
          multiHeader,
          header,
          merges,
          data,
          cWidth,
          rowCount: 1,
          fontSize,
          border,
          cellStyle,
          filename
        })

        this.excelLoading = false
      })
    }
  },
  mounted () {

  }
}
</script>

<style scoped>
</style>
